import gtk
import pango
import obj_types.obj_image
import colors

class ColoredButton(gtk.Button):
	def __init__(self, caption = "",width=75,height=30):
		gtk.Button.__init__(self, caption)
		self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(colors.lightgreen))
		self.modify_bg(gtk.STATE_ACTIVE, gtk.gdk.color_parse(colors.green))
		self.modify_bg(gtk.STATE_PRELIGHT, gtk.gdk.color_parse(colors.lightgreen))
		self.modify_bg(gtk.STATE_SELECTED, gtk.gdk.color_parse(colors.yellow))
		self.set_size_request(width, height)

class ErrorDisp(gtk.EventBox):
	parent = None
	
	def __init__(self, parent, message):
		gtk.EventBox.__init__(self)
		self.parent = parent

		self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse(colors.black))
		
		box = gtk.VBox(False, 10)
		box.set_border_width(10)
		box.show()
		self.add(box)

		l_message = gtk.Label(message)
		l_message.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(colors.yellow))
		l_message.show()
		box.pack_start(l_message, False, False, 0)
		
		box_buttons = gtk.HBox(False, 3)
		box_buttons.show()
		box.pack_start(box_buttons, False, False, 0)
		
		btn_ok = ColoredButton("OK")
		box_buttons.pack_end(btn_ok, True, False, 0)
		btn_ok.connect("clicked", lambda wid: self.parent.display_login())
		btn_ok.show()
		
def get_text(obj):
	textview = gtk.TextView()
	textview.set_editable(False)
	textview.set_cursor_visible(False)
	buffer = textview.get_buffer()

	bold = buffer.create_tag()
	bold.set_property("weight",  pango.WEIGHT_BOLD)
	italic = buffer.create_tag()
	italic.set_property("style", pango.STYLE_ITALIC)
	underline = buffer.create_tag()
	underline.set_property("underline", pango.UNDERLINE_SINGLE)	
	
	for text_obj in obj:
		sub = buffer.create_tag()
		sub.set_property("rise", - text_obj.size * 400)
		sup = buffer.create_tag()
		sup.set_property("rise", text_obj.size * 600)
		font = buffer.create_tag()
		font.set_property("font", text_obj.font)
		size = buffer.create_tag()
		size.set_property("size-points", (text_obj.size, text_obj.size / 2.0)[text_obj.superscript | text_obj.subscript] )
		color = buffer.create_tag()
		try:
			color.set_property("foreground", text_obj.color)
		except:
			color.set_property("foreground", 'black')
		styles = [bold, italic, underline, sub, sup]
		values = [text_obj.bold, text_obj.italic, text_obj.underline, text_obj.subscript, text_obj.superscript]
		buffer.insert_with_tags(buffer.get_end_iter(), text_obj.text, font, size, color, *[x for x, y in zip(styles, values) if y])

	return textview

def get_image(obj):
		
	pixbuf = gtk.gdk.pixbuf_new_from_file(obj.src)
	scale_x = float(obj.width) / pixbuf.get_width()
	scale_y = float(obj.height) / pixbuf.get_height()
	scaled = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, obj.width, obj.height)
	pixbuf.scale(scaled, 0, 0, obj.width, obj.height, 0, 0, scale_x, scale_y, gtk.gdk.INTERP_BILINEAR)

	image = gtk.Image()
	image.set_from_pixbuf(scaled)
	if obj.align == obj_types.obj_image.Image.ALIGN_LEFT:
		image.set_alignment(0.0, 0.5)
	elif obj.align == obj_types.obj_image.Image.ALIGN_CENTER:
		image.set_alignment(0.5, 0.5)
	elif obj.align == obj_types.obj_image.Image.ALIGN_RIGHT:
		image.set_alignment(1.0, 0.5)

	return image
	
class Disp(gtk.EventBox):
	def __init__(self, obj_list):
		gtk.EventBox.__init__(self)
		bgcolor = gtk.gdk.color_parse(obj_list[0])
		self.modify_bg(gtk.STATE_NORMAL, bgcolor)
		
		page = gtk.VBox(False, 0)
		self.add(page)
		page.set_border_width(20)
		page.show()
		
		title = get_text(obj_list[1])
		page.pack_start(title, False, False, 15)
		title.modify_base(gtk.STATE_NORMAL, bgcolor)
		buffer = title.get_buffer()
		center = buffer.create_tag()
		center.set_property("justification", gtk.JUSTIFY_CENTER)
		buffer.apply_tag(center, buffer.get_start_iter(), buffer.get_end_iter())
		title.show()
		
		for obj in obj_list[2:]:
			if isinstance(obj, obj_types.obj_image.Image):
				image = get_image(obj)
				page.pack_start(image, False, False, 0)
				image.show()
			elif obj is not None:
				text = get_text(obj)
				page.pack_start(text, False, False, 0)
				text.modify_base(gtk.STATE_NORMAL, bgcolor)
				buffer = text.get_buffer()
				indent = buffer.create_tag()
				indent.set_property("indent", 40)
				text.set_wrap_mode(gtk.WRAP_WORD)
				buffer.apply_tag(indent, buffer.get_start_iter(), buffer.get_end_iter())
				text.show()
